package com.night.icm.controller.business;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.model.business.NotificationInformationDTO;
import com.night.icm.service.business.service.NotificationInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * NotificationInformationController
 *
 * @author Liao ZhiYong
 * @date 2020/4/1
 */
@Api(tags = "通知管理")
@RestController
@RequestMapping("/api/icm-notification-information")
public class NotificationInformationController {

    private final NotificationInformationService notificationInformationService;

    @Autowired
    public NotificationInformationController(NotificationInformationService notificationInformationService) {
        this.notificationInformationService = notificationInformationService;
    }


    @ApiOperation(value = "分页查找通知信息")
    @GetMapping(value = "")
    public PageDataDTO<NotificationInformationDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页记录条数", required = true, defaultValue = "30") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段，例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "通知标题") @PathVariable(value = "title", required = false) String title,
            @ApiParam(value = "通知类型") @PathVariable(value = "type", required = false) String type,
            @ApiParam(value = "通知对象") @PathVariable(value = "receiverName", required = false) String receiverName
    ) {
        return this.notificationInformationService.findOnePage(page, size, sort, title, type, receiverName);
    }

    @ApiOperation(value = "新建通知")
    @PostMapping(value = "")
    public NotificationInformationDTO create(@ApiParam(value = "通知信息", required = true) @RequestBody @Validated NotificationInformationDTO notificationInformationDTO) {
        return this.notificationInformationService.create(notificationInformationDTO);
    }

    @ApiOperation(value = "根据接收人id查找通知")
    @GetMapping(value = "/receiver/{id}")
    public List<NotificationInformationDTO> findByReceiverId(@ApiParam(value = "接收者id", required = true) @PathVariable(value = "id") String id) {
        return this.notificationInformationService.findByReceiverId(id);
    }

    @ApiOperation(value = "通过id查找通知")
    @GetMapping(value = "/{id}")
    public NotificationInformationDTO findById(@ApiParam(value = "通知id", required = true) @PathVariable(value = "id") String id) {
        return this.notificationInformationService.findById(id);
    }

    @ApiOperation(value = "删除通知")
    @DeleteMapping(value = "/{id}")
    public void deleteById(@ApiParam(value = "通知id", required = true) @PathVariable(value = "id") String id) {
        this.notificationInformationService.delete(id);
    }

    @ApiOperation(value = "改变通知状态")
    @PutMapping("/change-status/{id}")
    public Boolean changeStatus(
            @ApiParam(value = "id", required = true) @PathVariable(value = "id") String id
    ) {
        return this.notificationInformationService.changeStatus(id);
    }
}